package com.guigusuqi.act.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigusuqi.act.dto.PublishActivityInfoDTO;
import com.guigusuqi.act.dto.UserActivityVo;
import com.guigusuqi.act.entity.ActPublish;
import com.guigusuqi.act.entity.UserActivity;
import com.guigusuqi.act.mapper.UserActivityMapper;
import com.guigusuqi.act.service.ActPublishService;
import com.guigusuqi.act.service.UserActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigusuqi.commonutils.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-15
 */
@Service
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements UserActivityService
{
    @Resource
    private UserActivityMapper userActivityMapper;

    @Resource
    private ActPublishService actPublishService;

    @Override
    public Result actRegistration(PublishActivityInfoDTO publishActivityInfoDTO)
    {
        UserActivity userActivity = new UserActivity();
        userActivity.setOpenid(publishActivityInfoDTO.getOpenid());
        userActivity.setActId(publishActivityInfoDTO.getActId());
        userActivity.setPublishNumber(publishActivityInfoDTO.getPublishNumber());
        userActivity.setStatus(0);
        int insert = userActivityMapper.insert(userActivity);
        if(insert >= 1)
        {
            return Result.success().message("活动报名成功");
        }else
        {
            return Result.fail().message("活动报名失败");
        }
    }

    @Override
    public int selectRegisterActivityCount(String openid, String actId)
    {
        LambdaQueryWrapper<UserActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserActivity::getOpenid,openid);
        queryWrapper.eq(UserActivity::getActId,actId);
        return userActivityMapper.selectCount(queryWrapper);
    }

    @Override
    public Result selectRegisteredActivity(String openid)
    {
        LambdaQueryWrapper<UserActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserActivity::getOpenid,openid);
        List<UserActivity> userActivities = userActivityMapper.selectList(queryWrapper);
        List<ActPublish> actPublishes = new ArrayList<>();
        for (UserActivity userActivity : userActivities)
        {
            ActPublish a = actPublishService.getById(userActivity.getActId());
            actPublishes.add(a);
        }
        return Result.success().data("rows",actPublishes);
    }

    @Override
    public Result deleteRegisteredActivity(String openid, String actId) {
        QueryWrapper<UserActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        queryWrapper.eq("act_id",actId);
        UserActivity userActivity = getOne(queryWrapper);
        if(userActivity==null) return Result.fail().data("rows","操作失败,未报名该活动");
        // 该用户本次报名的所报名人数
        Integer number = userActivity.getPublishNumber();
        // 该活动act_id
        String act_id = userActivity.getActId();

        // 报名表id
        String id = userActivity.getId();

        ActPublish actPublish = actPublishService.getById(act_id);
        if(actPublish==null) return Result.fail().data("rows","操作失败,未找到该活动");

        int nowLimitNumber = actPublish.getLimitNumber() + number;
        actPublish.setLimitNumber(nowLimitNumber);
        boolean update = actPublishService.updateById(actPublish);
        boolean remove = removeById(id);
        if(update&&remove) return Result.success().data("rows","操作成功");
        else return Result.fail().data("rows","操作失败");
    }

    @Override
    public Result selectUserActList(String openid) {


        List<UserActivityVo> res = userActivityMapper.selectUserActList(openid);
        if(res.isEmpty()){
            return Result.fail().data("rows","找不到对应活动");
        }
        else return Result.success().data("rows",res);
    }

    @Override
    public Result selectUserActStatus(String openid, String actId) {
        QueryWrapper<UserActivity> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        queryWrapper.eq("act_id", actId);
        List<UserActivity> res = list(queryWrapper);
        if(res.isEmpty()){
            return Result.fail().data("rows","未报名");
        }
        else return Result.success().data("rows",res);
    }
}
